package com.tairan.chapter.security.service.impl;

import com.google.common.collect.Lists;
import com.tairan.chapter.security.model.Role;
import com.tairan.chapter.security.model.User;
import com.tairan.chapter.security.service.IUserService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService, UserDetailsService {
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = new User();
        user.setId(1L);
        user.setUsername("litairan");
        user.setPassword(new BCryptPasswordEncoder().encode("litairan"));
        List<Role> roles = Lists.newArrayList();
        Role role = new Role();
        role.setId(1L);
        role.setCode("ROLE_ADMIN");
        role.setName("超级管理员");
        roles.add(role);
        user.setRoles(roles);
        return user;
    }

    public static void main(String[] args) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String litairan = encoder.encode("litairan");
        System.out.println("$2a$10$VIywRSa7ApQYzu7pB/eIo.alwxsbI2h88jji/.dvlynGNkYaRUP2W");
        System.out.println(litairan);
        System.out.println(encoder.encode("litairan"));
    }
}
